<template lang="pug">
	.select-parent-wrap
		.parent-list
			.field-item(v-for="(item,index) in list" :key="index" @click="authentication(index)")
				.left
					.role {{item.familyRelation}}
					.name {{item.name}}
				.right
					.hint(:class="{'active':item.realState==='1'}") {{item.realState==='1'?'已认证':'未认证'}}
					i.arrow-right
		.select-btn(@click="addParent")
			main-button(text="其他家长" :isActive="true")
</template>

<script>
import mainButton from '@/components/main-button'
export default {
	name: 'selectParent',
	data() {
		return {
			list: []
		}
	},
	created() {},
	mounted() {
		let familyRole = this.wx.getStorageSync('familyRole')
		this.mainHealthId = this.wx.getStorageSync('userInfo').memberNum
		this.name = familyRole.name
		this.idNumber = familyRole.idNumber
		this.role = familyRole.role
		this.phone = familyRole.phone
		this.selectFamily()
	},
	methods: {
		selectFamily() {
			this.api.tzApi
				.selectFamily({
					mainHealthId: this.mainHealthId
				})
				.then(data => {
					if (data.responseFlag === '1') {
						let list = data.data.filter(item => item.isgrouup === '1')
						this.list = list
					} else {
						this.wx.showToast({
							title: data.responseMessage,
							icon: 'none'
						})
					}
				})
				.catch(() => {})
		},
		judgeHouseholdRegister(item) {
			this.api.oneCardApi
				.judgeHouseholdRegister({
					idNumber: this.idNumber,
					parentIdNumber: item.idNumber
				})
				.then(data => {
					if (data.responseFlag === '1') {
						this.insertFamily(item)
						//领卡
					} else {
						this.wx.showToast({
							title: data.responseMessage,
							icon: 'none'
						})
					}
				})
		},
		insertFamily(item) {
			this.api.tzApi
				.insertFamily({
					mainHealthId: this.mainHealthId,
					familyRelation: this.role,
					idNumber: this.idNumber,
					name: this.name,
					phone: this.phone,
					parentIdNumber: item.idNumber,
					parentName: item.name
				})
				.then(data => {
					if (data.responseFlag === '1') {
					} else {
						this.wx.showToast({
							title: data.responseMessage,
							icon: 'none'
						})
					}
				})
				.catch(() => {})
		},
		authentication(index) {
			let item = this.list[index]
			if (item.realState === '1') {
				//家人已认证
				this.judgeHouseholdRegister(item)
			} else {
				this.wx.setStorage({
					key: 'familyRole',
					data: {
						name: this.name,
						idNumber: this.idNumber,
						role: this.role,
						phone: this.phone,
						parentIdNumber: item.idNumber,
						parentName: item.name
					}
				})
				this.wx.showToast({
					title: '该家长需先进行认证',
					icon: 'none'
				})
				setTimeout(() => {
					this.$router.push({
						path: '/views/faceAuthentication/uploadIdPhoto'
					})
				}, 500)
			}
		},
		addParent() {
			this.$router.push({
				path: '/views/faceAuthentication/addParentInfo'
			})
		}
	},
	components: {
		mainButton
	}
}
</script>

<style lang="stylus">
	.select-parent-wrap
		.parent-list
			background-color white
			.field-item
				border-bottom borderStyle
				height 100px
				display flex
				align-items center
				justify-content space-between
				margin-left mainPadding
				padding-right mainPadding
				.left
					display flex
					font-size 28px
					line-height 40px
					.role
						color blackFontColor4C
					.name
						color blackFontColor1A
						padding-left 60px
				.right
					display flex
					align-items center
					.hint
						width 110px
						height 32px
						background rgba(253,175,61,1)
						border-radius 16px
						font-size 22px
						text-align center
						color white
						background-color #B6C1D0
						&.active
							background-color #FDAF3D
					.arrow-right
						margin-left 16px
						width 16px
						height 24px
						centerBackground("../../assets/image/common/arrow_right_grey.png")
				&:last-child
					border-bottom-width 0
		.select-btn
			width 690px
			height 92px
			margin 60px auto 0
			border-radius 46px
			overflow hidden

</style>
